using GroupSix.Domain.Repositories;
using GroupSix.Domain.Entity.App;
using GroupSix.Domain.common;
using GroupSix.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace GroupSix.Infrastructure.Reposoitories;

/// <summary>
/// 系统设置日志仓储实现
/// </summary>
public class SystemSettingLogRepository : EfRepository<GsSystemSettingLog>, ISystemSettingLogRepository
{
    public SystemSettingLogRepository(GroupSixDbContext context) : base(context)
    {
    }

    /// <summary>
    /// 根据设置键获取日志列表
    /// </summary>
    public async Task<PageResult<GsSystemSettingLog>> GetLogsBySettingKeyAsync(string settingKey, int page = 1, int pageSize = 20)
    {
        var queryable = _tb
            .Where(l => l.SettingKey == settingKey)
            .OrderByDescending(l => l.CreatedAt);

        var totalCount = await queryable.CountAsync();

        var items = await queryable
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ToListAsync();

        return new PageResult<GsSystemSettingLog>
        {
            Items = items,
            TotalCount = totalCount,
            PageIndex = page,
            PageSize = pageSize
        };
    }

    /// <summary>
    /// 根据操作人获取日志列表
    /// </summary>
    public async Task<PageResult<GsSystemSettingLog>> GetLogsByOperatorAsync(Guid operatorId, int page = 1, int pageSize = 20)
    {
        var queryable = _tb
            .Where(l => l.OperatorId == operatorId)
            .OrderByDescending(l => l.CreatedAt);

        var totalCount = await queryable.CountAsync();

        var items = await queryable
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ToListAsync();

        return new PageResult<GsSystemSettingLog>
        {
            Items = items,
            TotalCount = totalCount,
            PageIndex = page,
            PageSize = pageSize
        };
    }

    /// <summary>
    /// 获取最近的日志列表
    /// </summary>
    public async Task<PageResult<GsSystemSettingLog>> GetRecentLogsAsync(int page = 1, int pageSize = 20)
    {
        var queryable = _tb
            .OrderByDescending(l => l.CreatedAt);

        var totalCount = await queryable.CountAsync();

        var items = await queryable
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ToListAsync();

        return new PageResult<GsSystemSettingLog>
        {
            Items = items,
            TotalCount = totalCount,
            PageIndex = page,
            PageSize = pageSize
        };
    }
} 